package com.fcgzs.springboot.controller;

import com.fcgzs.springboot.service.ReportService;
import com.fcgzs.springboot.util.Result;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/report")
public class ReportController {
    //会员统计、套餐统计、运营数据统计、运营数据导出
    @Reference
    private ReportService reportService;
    @RequestMapping("/getMemberReport")
    public Result getMemberReport(){
        return reportService.getMemberReport();
    }

    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport(){
        return reportService.getSetmealReport();
    }

    @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData(){
        return reportService.getBusinessReportData();
    }

    @RequestMapping("/exportBusinessReport")
    public Result exportBusinessReport(HttpServletResponse response, HttpServletRequest request){
        //存储到文件服务器
        String path = "E:\\缓存区\\上传路径\\upload\\report_template.xlsx";
        //数据源
        Map<String,Object> map = (Map<String,Object>)reportService.getBusinessReportData().getData();
        //POI
        try {
            //xlsx文档
            XSSFWorkbook workbook  = new XSSFWorkbook(new FileInputStream(path));
            //sheet页
            XSSFSheet sheetAt = workbook.getSheetAt(0);
            //单元格赋值
            sheetAt.getRow(2).getCell(5).setCellValue((String) map.get("reportDate"));

            sheetAt.getRow(4).getCell(5).setCellValue((Integer)map.get("todayNewMember"));
            sheetAt.getRow(4).getCell(7).setCellValue((Integer)map.get("totalMember"));

            sheetAt.getRow(5).getCell(5).setCellValue((Integer)map.get("thisWeekNewMember"));
            sheetAt.getRow(5).getCell(7).setCellValue((Integer)map.get("thisMonthNewMember"));

            sheetAt.getRow(7).getCell(5).setCellValue((Integer)map.get("todayOrderNumber"));
            sheetAt.getRow(7).getCell(7).setCellValue((Integer)map.get("todayVisitsNumber"));

            sheetAt.getRow(8).getCell(5).setCellValue((Integer)map.get("thisWeekOrderNumber"));
            sheetAt.getRow(8).getCell(7).setCellValue((Integer)map.get("thisWeekVisitsNumber"));

            sheetAt.getRow(9).getCell(5).setCellValue((Integer)map.get("thisMonthOrderNumber"));
            sheetAt.getRow(9).getCell(7).setCellValue((Integer)map.get("thisMonthVisitsNumber"));

            System.out.println(1);
            List<Map<String,Object>> list = (List<Map<String,Object>>)map.get("hotSetmeal");
            System.out.println(2);
            int i = 12;
            for (Map<String,Object> hotSetmeal :list) {
                sheetAt.getRow(i).getCell(4).setCellValue((String) hotSetmeal.get("name"));
                sheetAt.getRow(i).getCell(5).setCellValue(hotSetmeal.get("setmeal_count") +"");
                sheetAt.getRow(i).getCell(6).setCellValue(hotSetmeal.get("proportion") +"");
                sheetAt.getRow(i).getCell(7).setCellValue("无");
                i++;
            }

            /* 通过浏览器进行下载: ServletOutputStream outputStream = response.getOutputStream();
               下载内容存放位置：FileOutPutStream outputStream= new FileOutputStream("指定位置"); */
            ServletOutputStream outputStream = response.getOutputStream();
            //文件响应excel类型
            response.setContentType("application/vnd.ms-excel");
            //设置下载文件名，以附件形式下载
            response.setHeader("content-Disposition", "attachment;filename=business.xlsx");//
            workbook.write(outputStream);
            outputStream.flush();
            outputStream.close();
            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  null;
    }
}
